/**
 * 微信-关键词
 */
(function($,window,document,undefined){
    var comChart;
    var WechatRelatewords = function(el,opts){
        this.el = el;
        this.defaults = {
            loginStatus:false,
            theme:'macarons',
            chartOption: {
                tooltip: {
                    trigger: 'item',
                    formatter: function(params) {
                        if (params.indicator2) { // is edge
                            return params.indicator2 + ' ' + params.name + ' ' + params.indicator;
                        } else { // is node
                            return params.name
                        }
                    }
                },
                legend: {
                    data: []
                },
                series: [{
                    type: 'chord',
                    ribbonType: false,
                    radius: '60%',
                    itemStyle: {
                        normal: {
                            label: {
                                rotate: true
                            }
                        }
                    },
                    // 使用 nodes links 表达和弦图
                    nodes: [],
                    links: []
                }]
            },
            showdata:{}
        };
        this.options = $.extend({},this.defaults,opts)
    };

    WechatRelatewords.prototype = {
        init: function(){
            var me = this,
                el = me.el;

            if(el.attr('data-initialized') == true){
                return me;
            }



            //获得数据
            me.initChart('dianshi');//default

            //tab 展示不同类型关键词
            $('#wechat-keywords-tab li a').bind('click',function(){
                //alert($(this).attr('type'));
                me.initChart($(this).attr('type'));
            });
            me.bindEvents();

            el.attr('data-initialized', 'true');
            return me;
        },

        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function(){
            var me = this,
                el = me.el;
        },

        formatData: function(originData,type){
            var me = this,
                el = me.el,
                chartOption = me.options.chartOption,
                legend = [],
                t = type,
                i, j, len1, len2;

            // 初始化legend
            for (i = 0, len1 = originData[t].length; i < len1; i++) {
                if (legend.indexOf(originData[t][i].keyword1) == -1) {
                    legend.push(originData[t][i].keyword1);
                }

                if (legend.indexOf(originData[t][i].keyword2) == -1) {
                    legend.push(originData[t][i].keyword2);
                }
            }
            chartOption.legend.data = legend;

            // 初始化nodes
            for (i = 0, len1 = legend.length; i < len1; i++) {
                chartOption.series[0].nodes.push({
                    name: legend[i]
                });
            }

            // 初始化links
            for (i = 0, len1 = originData[t].length; i < len1; i++) {
                chartOption.series[0].links.push({
                    source: originData[t][i].keyword1,
                    target: originData[t][i].keyword2,
                    weight: originData[t][i].weight,
                    name: '关联'
                });

                chartOption.series[0].links.push({
                    source: originData[t][i].keyword2,
                    target: originData[t][i].keyword1,
                    weight: originData[t][i].weight,
                    name: '关联'
                });
            }

            return chartOption;
        },

        initChart: function(type) {
            var me = this,
                el = me.el,
                option = {};
            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data,type);
            comChart.setOption(option);

//            $.ajax({
//                url: $.fn.utils.env == 'dev' ? '/keywords/1/wechat_relate_words.json' : '',
//                type: 'GET',
//                dataType: 'json',
//                data: {},
//                success: function(ret) {
//                    if (ret && ret.status) {
//                        var data = ret.data[0].wechat_relate_words;
//
//                        option = me.formatData(data,type);
//                        comChart.setOption(option);
//                    } else {
//
//                    }
//                },
//                error: function(ret) {
//
//                }
//            });

        }
    };

    $.fn.WechatRelatewords = function(opts){
        var me = this,
            el = me.el,
            comWechatRelatewords;

        // 如果没有被初始化过
        if(!(el && el.length && el.attr('data-initialized') != 'true')){
            comWechatRelatewords = new WechatRelatewords(this,opts);
        }

        return comWechatRelatewords.init();
    };

})(jQuery,window,document);